<template>
  <div>
    <!-- 新增一级菜单 -->
    <div class="addMainMenu">
      <el-button type="primary" icon="el-icon-plus" @click="addMainMenu"
        >新增</el-button
      >
    </div>
    <!-- 2、分隔线区域 -->
    <el-divider></el-divider>
    <!-- 3、表单 -->
    <el-table
      :data="menuData"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :tree-props="{ children: 'childrens', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="菜单名称" width="180">
      </el-table-column>
      <el-table-column prop="type" label="菜单类型" width="180">
        <template slot-scope="scope">
          <!--获取type属性，进行条件判断-->
          <el-tag v-if="scope.row.type === 1">菜单</el-tag>
          <el-tag type="info" v-else>目录</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标" width="100">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="路由地址" width="180">
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="80"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column prop="disable" label="启用/禁用" width="180">
        <template slot-scope="scope">
          <el-switch
            @change="disableChange(scope.row)"
            v-model="scope.row.disable"
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="禁用"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            v-if="!scope.row.url"
            icon="el-icon-plus"
            size="mini"
            @click="addChildren(scope.row)"
            title="新增"
          />
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="updChildren(scope.row)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteMenu(scope.row.id)"
            title="删除"
            v-if="!scope.row.childrens"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增主菜单弹框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form">
        <el-form-item
          v-if="form.parentName !== undefined"
          label="上级目录"
          prop="parentName"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.parentName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth" required>
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth" required>
          <el-select
            v-model="form.icon"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="(item, index) in iconOptions"
              :key="index"
              :value="item.class"
            >
              <span>
                <i :class="item.class"></i>
              </span>
              <span style="padding-left: 6px">{{ item.class }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路由地址" :label-width="formLabelWidth">
          <el-input
            v-model="form.url"
            autocomplete="off"
            :disabled="form.type === 0"
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth" required>
          <el-select
            v-model="form.type"
            :disabled="form.typeDisabled"
            placeholder="请选择"
          >
            <el-option
              v-for="item in type"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <!--获取type属性，进行条件判断-->
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth" required>
          <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="状态"
          prop="disable"
          :label-width="formLabelWidth"
          required
        >
          <el-switch
            v-model="form.disable"
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="禁用"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="addOrUpd()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserMenuList,
  add,
  upd,
  delMenuById,
  updDisable,
} from "@/services/menu";
export default {
  name: "Menu",
  data() {
    return {
      formLabelWidth: "120px",
      dialogVisible: false,
      dialogTitle: "",
      form: {
        name: "",
        pid: "0",
        icon: "",
        url: null,
        sort: null,
        disable: 0,
        typeDisabled: false,
        type: null,
        handleType: "ADD",
      },
      menuData: [],
      iconOptions: [
        {
          class: "el-icon-s-tools",
        },
        {
          class: "el-icon-s-custom",
        },
        {
          class: "el-icon-setting",
        },
        {
          class: "el-icon-user-solid",
        },
        {
          class: "el-icon-s-help",
        },
        {
          class: "el-icon-phone",
        },
        {
          class: "el-icon-s-unfold",
        },
        {
          class: "el-icon-s-operation",
        },
        {
          class: "el-icon-more-outline",
        },
        {
          class: "el-icon-s-check",
        },
        {
          class: "el-icon-tickets",
        },
        {
          class: "el-icon-s-goods",
        },
        {
          class: "el-icon-document-remove",
        },
        {
          class: "el-icon-warning",
        },
        {
          class: "el-icon-warning-outline",
        },
        {
          class: "el-icon-question",
        },
        {
          class: "el-icon-info",
        },
        {
          class: "el-icon-message",
        },
        {
          class: "el-icon-chat-line-round",
        },
        {
          class: "el-icon-chat-line-square",
        },
        {
          class: "el-icon-s-platform",
        },
      ],
      type: [
        {
          name: "目录",
          value: 0,
        },
        {
          name: "菜单",
          value: 1,
        },
      ],
    };
  },

  mounted() {
    this.getUserMenuList();
  },

  methods: {
    getUserMenuList() {
      getUserMenuList()
        .then((res) => {
          this.menuData = res.data;
        })
        .catch((error) => {
          console.error(error);
        });
    },
    getParentInfo(childrenId) {
      const hasChildrens = this.menuData.filter((i) => i.url == null);
      var parentInfo;
      hasChildrens.forEach((item) => {
        //遍历子节点
        if (item.childrens) {
          item.childrens.forEach((item2) => {
            //判断该节点是否为我点击的节点
            if (item2.id === childrenId) {
              parentInfo = item;
            }
          });
        }
      });
      return parentInfo;
    },
    // 新增子菜单弹框
    addChildren(menuInfo) {
      this.clearFrom();
      this.dialogVisible = true;
      this.form.typeDisabled = true;
      this.form.type = 1;
      this.form.parentName = menuInfo.name;
      this.form.pid = menuInfo.id;
      this.dialogTitle = "添加目录/菜单节点";
    },
    // 修改子菜单弹框
    updChildren(menuInfo) {
      this.dialogVisible = true;
      this.form.id = menuInfo.id;
      this.form.name = menuInfo.name;
      this.form.icon = menuInfo.icon;
      this.form.url = menuInfo.url;
      this.form.type = menuInfo.type;
      this.form.sort = menuInfo.sort;
      this.form.disable = menuInfo.disable;
      //其他字段
      this.form.handleType = "UPD";
      this.form.typeDisabled = true;
      if (menuInfo.url) {
        let parentInfo = this.getParentInfo(menuInfo.id);
        if (menuInfo.type === 1 && parentInfo) {
          this.form.parentName = parentInfo.name;
          this.form.pid = parentInfo.id;
        }
        this.dialogTitle = "修改菜单节点";
      } else {
        this.dialogTitle = "修目录节点";
      }
    },
    // 新增主菜单弹框
    addMainMenu() {
      this.clearFrom();
      this.dialogTitle = "添加目录/菜单节点";
      this.dialogVisible = true;
    },
    //启用/禁用
    disableChange(menuInfo) {
      updDisable(menuInfo.id, menuInfo.disable)
        .then((res) => {
          if (res.data) {
            this.$message({
              type: "success",
              message: "修改成功",
            });
          }
        })
        .catch((error) => {});
    },
    close() {
      this.dialogVisible = false;
      this.clearFrom();
    },
    //新增或者修改
    addOrUpd() {
      //新增
      if (this.form.handleType === "ADD") {
        add(this.form)
          .then((res) => {
            this.$message({
              type: "success",
              message: "新增成功",
            });
            this.dialogVisible = false;
            this.clearFrom();
            this.getUserMenuList();
          })
          .catch((error) => {});
      } else {
        //修改
        upd(this.form)
          .then((res) => {
            this.$message({
              type: "success",
              message: "修改成功",
            });
            this.dialogVisible = false;
            this.clearFrom();
            this.getUserMenuList();
          })
          .catch((error) => {});
      }
    },

    deleteMenu(id) {
      this.$confirm("是否要删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delMenuById(id)
            .then((res) => {
              if (res.data) {
                this.$message({
                  type: "success",
                  message: "删除成功",
                });
                this.dialogVisible = false;
                this.getUserMenuList();
              }
            })
            .catch((error) => {});
          // this.submitRuleBaseSwitch(row, val); 调用接口修改数据
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "已取消！",
          });
        });
    },
    clearFrom() {
      this.form = {
        parentName: "",
        name: "",
        pid: 0,
        icon: "",
        url: null,
        sort: null,
        disable: 0,
        typeDisabled: false,
        type: null,
        handleType: "ADD",
      };
    },
  },
};
</script>

<style lang="scss" scoped>
</style>